package cn.byapps.hw.service.impl;

import cn.byapps.hw.dao.SerialPKDao;
import cn.byapps.hw.dao.TimetableDao;
import cn.byapps.hw.entity.*;
import cn.byapps.hw.service.SerialPKService;
import cn.byapps.hw.utils.DataUtils;
import cn.byapps.hw.vo.ClazzTimetableData;
import cn.byapps.hw.vo.VueData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SerialPKServiceImpl implements SerialPKService {

    @Resource
    private SerialPKDao serialPKDao;
    @Resource
    private TimetableDao timetableDao;

    @Override
    public List<SerialPK> getClazzbyTeacher(Teacher teacher) {
        return serialPKDao.getClazzByTeacher(teacher);
    }

    @Override
    public List<SerialPK> getClazzByTeacherNums(Clazz clazz) {
        return serialPKDao.getClazzByTeacherNums(clazz);
    }

    @Override
    public List<SerialPK> getCourse(Course course) {
        return serialPKDao.getCourse(course);
    }

    @Override
    public void save(SerialPK serialPK) {
        serialPKDao.save(new SerialPK(null, null, serialPK.getClazz(), serialPK.getCourse(), serialPK.getTeacher(), new Date()));
    }

    @Override
    public SerialPK getByTeacherAndCourse(Course course, Teacher teacher, Clazz clazz) {
        return serialPKDao.getByTeacherAndCourse(course, teacher, clazz);
    }

    @Override
    public void delete(SerialPK serialPK) {
        serialPKDao.delete(serialPK);
    }

    @Override
    public SerialPK getByClazzAndCourse(Integer courseId, Integer clazzId) {
        return serialPKDao.getByClazzAndCourse(courseId, clazzId);
    }

    @Override
    public VueData<String> addmore(ClazzTimetableData clazzTimetableData) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //格式化时间
        Date beginTime = format.parse(format.format(clazzTimetableData.getBeginTime()));
        Date endTime = format.parse(format.format(clazzTimetableData.getEndTime()));
        //获取时间当时的周数
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(beginTime);
        Integer beginWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(endTime);
        Integer endWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(beginTime);
        //获取课程时间表
        List<List<Integer>> courseTimeList = clazzTimetableData.getGetDay();
        //创建对应每一个星期日子的集合
        List<String> eachMonday = new ArrayList<>();
        List<String> eachTuesday = new ArrayList<>();
        List<String> eachWednesday = new ArrayList<>();
        List<String> eachThursday = new ArrayList<>();
        List<String> eachFriday = new ArrayList<>();
        //获取是否是单双周,1为单双周,2为单周,3为双周
        Integer week = clazzTimetableData.getWeek();
        for (int i = 0; i < courseTimeList.size(); i++) {
            if (courseTimeList.get(i).size() != 0) {
                int zhoushu = beginWeek;     //周数
                int n = 1;          //叠加次数
                if (week == 2) n = 2;
                else if (week == 3) {
                    n = 2;
                    zhoushu += 1;
                }
                for (int j = zhoushu; j < endWeek + 1; j = j + n) {
                    String data = DataUtils.getDayOfWeekNo(calendar.get(Calendar.YEAR), j, i);
                    if (i == 0) eachMonday.add(data);
                    if (i == 1) eachTuesday.add(data);
                    if (i == 2) eachWednesday.add(data);
                    if (i == 3) eachThursday.add(data);
                    if (i == 4) eachFriday.add(data);
                }

            }
        }
        List<Timetable> timetables = new ArrayList<>();
        for (int i = 0; i < courseTimeList.size(); i++) {
            if (courseTimeList.get(i).size() != 0) {
                for (Integer value : courseTimeList.get(i)) {
                    if (i == 0) for (String s : eachMonday) {
                        if (timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value) != null) {
                            timetables.add(timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value));
                        }
                    }
                    if (i == 1) for (String s : eachTuesday) {
                        if (timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value) != null) {
                            timetables.add(timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value));
                        }
                    }
                    if (i == 2) for (String s : eachWednesday) {
                        if (timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value) != null) {
                            timetables.add(timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value));
                        }
                    }
                    if (i == 3) for (String s : eachThursday) {
                        if (timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value) != null) {
                            timetables.add(timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value));
                        }
                    }
                    if (i == 4) for (String s : eachFriday) {
                        if (timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value) != null) {
                            timetables.add(timetableDao.getByClazzIdAAndDataAndNum(clazzTimetableData.getClazzId(), format.parse(s), value));
                        }
                    }
                }
            }
        }
        if (timetables.size() > 0) return new VueData<>("本班级在某一时间段上,拥有相同时间段的课程,请重新填写");
        //添加数据进数据库
        SerialPK serialPK = new SerialPK();
        if (serialPKDao.getByClazzAndCourse(clazzTimetableData.getCourseId(), clazzTimetableData.getClazzId()) != null) {
            serialPK = serialPKDao.getByClazzAndCourse(clazzTimetableData.getCourseId(), clazzTimetableData.getClazzId());
        } else {
            serialPK = serialPKDao.save(new SerialPK(null, null, new Clazz(clazzTimetableData.getClazzId(), null, null), new Course(clazzTimetableData.getCourseId(), null), new Teacher(clazzTimetableData.getTeacherId(), null, null, null), new Date()));
        }

        //SerialPK
        for (int i = 0; i < courseTimeList.size(); i++) {
            if (courseTimeList.get(i).size() != 0) {
                for (Integer value : courseTimeList.get(i)) {
                    if (i == 0) for (String s : eachMonday)
                        timetableDao.save(new Timetable(null, format.parse(s), "星期一", value, new SerialPK(serialPK.getId(), null, null, null, null, null)));
                    if (i == 1) for (String s : eachTuesday)
                        timetableDao.save(new Timetable(null, format.parse(s), "星期二", value, new SerialPK(serialPK.getId(), null, null, null, null, null)));
                    if (i == 2) for (String s : eachWednesday)
                        timetableDao.save(new Timetable(null, format.parse(s), "星期三", value, new SerialPK(serialPK.getId(), null, null, null, null, null)));
                    if (i == 3) for (String s : eachThursday)
                        timetableDao.save(new Timetable(null, format.parse(s), "星期四", value, new SerialPK(serialPK.getId(), null, null, null, null, null)));
                    if (i == 4) for (String s : eachFriday)
                        timetableDao.save(new Timetable(null, format.parse(s), "星期五", value, new SerialPK(serialPK.getId(), null, null, null, null, null)));
                }
            }
        }
        return new VueData<>();
    }


}
